package models;

import java.awt.Point;
import java.io.Serializable;

public class Board implements Serializable
{
	private final String name;
	private final Location[][] locations;
	private final int resourceCount;
	
	public Board(){
		this.name = "RandomBoard";
		this.locations = getRandomLocations();
		this.resourceCount = getRandomResourceCount();
	}
	
	private int getRandomResourceCount() {
		return (int) Math.random() * 100 + 1;
	}

	private Location[][] getRandomLocations() {
		
		int width = (int) (Math.random() * 10 + 1);
		int height = (int) (Math.random() * 10 + 1);
		
		Location[][] locations = new Location[width][height];
		
		for(int i = 0; i < width; i++){
			for(int j = 0; j < height; j++){
				locations[i][j] = new Location();
			}
		}
		
		return locations;
	}

	public Board(String name, Location[][] locations, int resourceCount)
	{
		this.name = name;
		this.locations = locations;
		this.resourceCount = resourceCount;
	}
	
	/**
	 * Returns the name of this board, used to uniquely identify the board.
	 * @return
	 */
	public String getName()
	{
		return name;
	}
	
	/**
	 * Gets the board Location at the given x,y position.
	 * @param x
	 * @param y
	 * @return
	 */
	public Location getLocation(int x, int y)
	{
		if(locations.length>x && locations[x].length > y)
			return locations[x][y];
		return null;
	}
	
	/**
	 * Gets the board Location at the given point.
	 * @param point
	 * @return
	 */
	public Location getLocation(Point point)
	{
		if(locations.length>point.x && locations[0].length > point.y)
			return locations[point.x][point.y];
		return null;
	}
	
	/**
	 * Gets how many Locations tall the board is.
	 * @return
	 */
	public int getHeight()
	{
		return locations[0].length;
	}
	
	/**
	 * Gets how many Locations wide the board is.
	 * @return
	 */
	public int getWidth()
	{
		return locations.length;
	}
	
	/**
	 * Returns the number of resources the board will start with.
	 */
	public int getResourceCount()
	{
		return resourceCount;
	}
	
	@Override
	public boolean equals(Object obj){
		if(this.getName().equals( ((Board)obj).getName() )){
			return true;
		}else{
			return false;
		}
		
	}
}
